﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using TyMES.Model;
using TyMES.TemporaryClass;
using TyMES.ViewModel.Record.PassStationRecordVMs;
using TyMES.PassStationCommunication;
using Microsoft.AspNetCore.Http.HttpResults;
using System.Threading.Tasks;
using TyMES.Communication;
using TyMES.Model.BaseInformation;
using Aliyun.OSS;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Policy;
using TyMES.Services;
using Microsoft.AspNetCore.Http;


namespace TyMES.Record.Controllers
{
    [AuthorizeJwtWithCookie]
    public partial class PassStationRecordController : BaseApiController
    {

        //private readonly HttpService _httpService;

        //public PassStationRecordController(HttpService httpService)
        //{
        //    _httpService = httpService;
        //}
        [ActionDescription("Sys.Search")]
        [HttpPost("[action]")]
        public IActionResult SearchPassStationRecord(TyMES.ViewModel.Record.PassStationRecordVMs.PassStationRecordSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<TyMES.ViewModel.Record.PassStationRecordVMs.PassStationRecordListVM>();
                vm.Searcher = searcher;
                return Content(vm.GetJson());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Export")]
        [HttpPost("[action]")]
        public IActionResult PassStationRecordExportExcel(TyMES.ViewModel.Record.PassStationRecordVMs.PassStationRecordSearcher searcher)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.Record.PassStationRecordVMs.PassStationRecordListVM>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("[action]")]
        public IActionResult PassStationRecordExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.Record.PassStationRecordVMs.PassStationRecordListVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }
            return vm.GetExportData();
        }

        [HttpPost("[action]")]
        public Task<Results<Ok<ResponseBase>, BadRequest<string>>> PassStation(PassStationRequestSup passStationRequestSup)
        {
            
              return _normalPassStationDomain.PassStation(passStationRequestSup);
            
        }

        [HttpPost("SaveModel")]
        public async Task<ActionResult> SaveModel(PrintStyle style)
        {
            try
            {
                var res = DC.Set<PrintStyle>().Where(e => e.Name == style.Name).ToList();
                if (res.Count == 0)
                {
                    style.CreateTime = DateTime.Now;
                    style.UpdateTime = style.CreateTime;
                    await DC.Set<PrintStyle>().AddAsync(style);
                }
                else
                {
                    var ele = res[0];
                    ele.Style = style.Style;
                    ele.Length = style.Length;
                    ele.Height = style.Height;
                    ele.Field1 = style.Field1;
                    ele.Field2 = style.Field2;
                    ele.Field3 = style.Field3;
                    ele.UpdateTime = DateTime.Now;
                }
                await DC.SaveChangesAsync();
                return Ok(new { Code = "0000" });
            }
            catch (Exception ex)
            {
                return Ok(new { Code = "0001", Mesg = ex.Message });
            }
        }

        [HttpPost("GetModels")]
        public List<PrintStyle> GetModels()
        {
            List<PrintStyle> styles = DC.Set<PrintStyle>().OrderByDescending(e => e.UpdateTime).ToList();
            return styles;
        }

        [HttpPost("GetLastModel")]
        public PrintStyle GetLastModel()
        {
            List<PrintStyle> styles = DC.Set<PrintStyle>().OrderByDescending(e => e.UpdateTime).ToList();
            return styles[0];
        }

        


    }
}


